<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="item in roleListArr"
        :key="item.roleId"
        class="role-item"
        :class="{active: item.roleId === activeIndex}"
        @click="highlight(item.roleId)"
      >
        <div class="role-info">
          <svg-icon class="icon" :icon-class="item.roleId === activeIndex ? 'user-active' :'user'" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editRole(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="deleRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <!-- 右盒子 -->
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                :data="item.children"
                :props="{label:'title'}"
                show-checkbox
                :default-expand-all="true"
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员列表(${rolseUserList.length})`" name="second">
          <div class="user-wrapper">
            <el-table
              :data="rolseUserList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { roleListAPI, getRoleDetailAPI, getTreeListAPI, getRoleClickAPI, delRoleAPI } from '@/api/role'
//! 调用递归函数
function addDisabled(treeList) {
  treeList.forEach(item => {
    item.disabled = true
    if (item.children) {
      addDisabled(item.children)
    }
  })
}
export default {
  name: 'Role',
  data() {
    return {
      roleListArr: [], // !渲染空数组
      activeIndex: 0, //! 判断动态类名的条件
      treeList: [], //! 树状列表
      activeName: 'tree', //! 默认显示的组件
      rolseUserList: [] //! 角色列表数据
    }
  },
  async mounted() {
    await this.roleList()
    await this.treeListRender()
    //! 等待异步任务结束完才可以执行后面的代码
    this.defaultRendering(this.roleListArr[0].roleId)
  },
  methods: {
    //! Default rendering
    async defaultRendering(id) {
      const res = await getRoleDetailAPI(id)
      // console.log(res.data.perms)
      // ! 树形标签遍历
      // console.log(this.$refs.tree)
      this.$refs.tree.forEach((item, index) => {
        item.setCheckedKeys(res.data.perms[index])
      })
    },
    //! 渲染左边列表
    async roleList() {
      const res = await roleListAPI()
      // console.log(res.data)
      this.roleListArr = [...res.data]
    },
    //! 获取当前角色下的成员的方法
    async getUesrList(id) {
      const res = await getRoleClickAPI(id)
      // console.log(res)
      this.rolseUserList = [...res.data.rows]
    },
    //! 点击控制高亮
    highlight(id) {
      this.activeIndex = id
      this.defaultRendering(id)
      this.getUesrList(id)
    },
    //! 树桩列表
    async  treeListRender() {
      const res = await getTreeListAPI()
      // console.log(res.data)
      this.treeList = [...res.data]
      //! 调用函数把树状列表传进去
      addDisabled(this.treeList)
    },
    //! 编辑角色
    editRole(roleId) {
      this.$router.push({
        path: '/roleAdd',
        query: {
          roleId
        }
      })
    },
    //! 删除角色
    async deleRole(id) {
      await delRoleAPI(id)
      this.$message({
        type: 'success',
        message: '删除成功'
      })
      this.roleList()
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
